using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Web;

namespace Adge.Common.Config
{
    public struct Cover
    {
        public Cover(string filename, string fullpath, string relationPath, string orirelationPath)
        {
            this._filename = filename;
            this._fullpath = fullpath;
            this._relationPath = relationPath;
            this._orirelationPath = orirelationPath;
        }

        private string _filename;

        public string Filename
        {
            get { return _filename; }
            set { _filename = value; }
        }        

        private string _fullpath;

        public string Fullpath
        {
            get { return _fullpath; }
            set { _fullpath = value; }
        }

        private string _relationPath;

        public string RelationPath
        {
            get { return _relationPath; }
            set { _relationPath = value; }
        }

        private string _orirelationPath;

        public string OriRelationPath
        {
            get { return _orirelationPath; }
            set { _orirelationPath = value; }
        }
    }

    public class CoverConfig
    {
        private string _imageDirectory = ConfigurationManager.AppSettings["CoverImageDirectory"];
        private string fileDirectory = ConfigurationManager.AppSettings["FileStoragePath"];


        public string ImageDirectory
        {
            get { return _imageDirectory; }
            set { _imageDirectory = value; }
        }

        public IList<Cover> GetImages()
        {
            IList<Cover> imglst = new List<Cover>();
            DirectoryInfo dirInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath(ImageDirectory));
            FileInfo[] images = FilterForImages(dirInfo.GetFiles());
            for (int imgIndex = 0; imgIndex < images.Length; imgIndex++)
            {
                imglst.Add(new Cover(Path.GetFileName(images[imgIndex].Name), images[imgIndex].Name, ImageDirectory + images[imgIndex].Name, GetOriImageRelPath(ImageDirectory + images[imgIndex].Name)));
            }
            return imglst;
        }

        private string GetOriImageRelPath(string thumbnailPath)
        {
            string fileFullName = string.Empty;
            using (StreamReader sr = File.OpenText(HttpContext.Current.Server.MapPath(Path.ChangeExtension(thumbnailPath, ".txt"))))
            {
                fileFullName = sr.ReadLine();
            }
            //fileFullName = fileFullName.Substring(AdgeConfig.FileSoragePhyPath.Length);
            return fileFullName;
        }

        public FileInfo[] FilterForImages(FileInfo[] images)
        {
            ArrayList newImagesList = new ArrayList(images.Length);
            List<string> exts = new List<string>(10);
            string[] strExts = { ".jpg", ".jpeg", ".png", ".gif" };
            for (int i = 0; i < strExts.Length; i++)
            {
                exts.Add(strExts[i]);
            }

            for (int imageindex = 0; imageindex <= images.Length - 1; imageindex++)
            {
                if (exts.Contains(Path.GetExtension(images[imageindex].Name.ToLower())))
                {
                    newImagesList.Add(images[imageindex]);
                }
            }
            return (FileInfo[])newImagesList.ToArray(typeof(FileInfo));
        }
    }
}
